<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/22
 * Time: 17:23
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Attesdefenseunleave extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $role_audit_status = input('role_audit_status');
            $where = [];
            $where['role_user_type'] = 2; // 国防大学生
            //用户名称
            if(isset($user_name) && $user_name != ''){
                $where['user_username'] = ['like', '%' . $user_name . '%'];
            }
            //审核状态
            if(isset($role_audit_status) && $role_audit_status != ''){
                $where['role_audit_status'] = $role_audit_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            // 获取总条数
            $count = Db::name('user_role')
                ->join('up_u_user', 'user_id = role_user_id', 'left')
                ->where($where)
                ->count();
            $lists = Db::name('user_role')
                ->join('up_u_user', 'user_id = role_user_id', 'left')
                ->field('role_id, role_user_id, user_username, role_time, role_audit_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('role_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 申请详情
     * @return mixed
     */
    public function applyDetail(){
        $role_id = input('role_id');
        $detail = Db::table('up_user_role')
            ->join('__U_USER__', 'role_user_id = user_id', 'left')
            ->join('__ASSN__', 'assn_id = role_user_st', 'left')
            ->join('__SCHOOL__', 'sc_id = role_user_school', 'left')
            ->field('user_username, role_time, role_audit_status, role_false_reason, 
            role_user_card, role_user_student_img, assn_name, sc_name')
            ->where('role_id', $role_id)
            ->find();
        $this->assign([
            'detail' => $detail
        ]);
        return $this->fetch('attesdefenseunleave/applydetail');
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function applyAllow(){
        $role_id = input('id');
        $update = [
            'role_audit_status' => 2,
            'role_attes_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_user_role')->where('role_id', $role_id)->update($update);
        if($res){
            //TODO:推送
            $user_id = Db::table('up_user_role')->where('role_id', $role_id)->value('role_user_id');
            $list = [];//需要推送的用户数组
            $send_user_id = (string)$user_id;
            array_push($list, $send_user_id);
            try{
                $this->Jpush(1, $list, '您提交的认证通过审核啦', ['role_user_id' => $user_id, 'status' => 1, 'send_type' => 6]);
            }catch(\Exception $e){
            }
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
    
    /**
     * 审核驳回
     * @return mixed|\think\response\Json
     */
    public function applyRefuse(){
        if(Request::instance()->isPost()){
            $id = input('role_id');
            $role_false_reason = input('role_false_reason');
            $length = mb_strlen($role_false_reason, 'UTF8');
            if($length > 100){
                return json(['code' => 100, 'msg' => '长度不可超过100个字']);
            }
            $update = [
                'role_audit_status' => 3,
                'role_false_reason' => $role_false_reason,
                'role_attes_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_user_role')->where('role_id', $id)->update($update);
            if($res){
                //TODO:推送
                $user_id = Db::table('up_user_role')->where('role_id', $id)->value('role_user_id');
                $list = [];//需要推送的用户数组
                $send_user_id = (string)$user_id;
                array_push($list, $send_user_id);
                try{
                    $this->Jpush(1, $list, '您提交的认证未通过审核', ['role_user_id' => $user_id, 'status' => 2, 'send_type' => 6]);
                }catch(\Exception $e){
                }
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $role_id = input('role_id');
        $this->assign('role_id', $role_id);
        return $this->fetch('attesdefenseunleave/applyrefuse');
    }
    
    /**
     * 删除提交
     * @return \think\response\Json
     */
    public function applyDel(){
        $role_id = input('id');
        $res = Db::table('up_user_role')->where('role_id', $role_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}